package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * Common super class for all CAPI messages.
 */
public abstract class CapiMessage {
    /**
     * Retrieve the total length of this message.
     *
     * @return The total length of this message.
     */
    public abstract short getTotalLength();

    /**
     * Retrieve the application ID associated with this message.
     *
     * @return The application ID.
     */
    public abstract short getAppId();

    /**
     * Retrieve the command.
     *
     * @return The command.
     */
    public abstract Command getCommand();

    /**
     * Retrieve the message number.
     *
     * @return The message number.
     */
    public abstract short getMessageNumber();

    /**
     * Retrieve the binary representation of this CAPI message.
     *
     * <p>The returned {@code ByteBuffer} contains the CAPI message starting
     * from index 0. The {@code ByteBuffer}'s position is set to 0 and limit
     * marks the end of the message.
     *
     * @return The binary representation of this CAPI message.
     */
    public abstract ByteBuffer getByteBuffer();

    /**
     * Build and return a human-readable {@code String} representation of
     * this CAPI message.
     *
     * @return A human-readable {@code String} representation of this CAPI
     *         message.
     */
    @Override
    public String toString() {
        String s = String.format(
                "header[len=%d appId=0x%04x cmd=%s msgNr=0x%04x]",
                getTotalLength(),
                getAppId(),
                getCommand(),
                getMessageNumber());

        return s;
    }

    /** CAPI message header starts at position 0. */
    protected static final int OFFSET_HEADER = 0;

    /** CAPI message contents start at position 8. */
    protected static final int OFFSET_MESSAGE = 8;

    /** The PLCI parameter is of type dword. */
    protected static final int SIZE_OF_PLCI = 4;

    /** The Controller parameter is of type dword. */
    protected static final int SIZE_OF_CONTROLLER = 4;

    /** The NCCI parameter is of type type dword. */
    protected static final int SIZE_OF_NCCI = 4;

}
